package com.linghui.videoplus.ipai.api.async;


import java.util.concurrent.Callable;


import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;


public class AsyncUtils {
	
	public static <T> void doAsync(final Context pContext, final int pTitleResID, final int pMessageResID, final Callable<T> pCallable, final Callback<T> pCallback) {
		AsyncUtils.doAsync(pContext, pTitleResID, pMessageResID, pCallable, pCallback, null, false);
	}
	public static <T> void doAsync(final Context pContext, final int pTitleResID, final int pMessageResID, final Callable<T> pCallable, final Callback<T> pCallback, final Callback<Exception> pExceptionCallback, final boolean pCancelable) {
		AsyncUtils.doAsync(pContext, pContext.getString(pTitleResID), pContext.getString(pMessageResID), pCallable, pCallback, pExceptionCallback, pCancelable);
	}
	
	public static <T> void doAsync(final Context pContext, final CharSequence pTitle, final CharSequence pMessage, final Callable<T> pCallable, final Callback<T> pCallback, final Callback<Exception> pExceptionCallback, final boolean pCancelable) {
		new AsyncTask<Void, Void, T>() {
			private Exception mException = null;
			
			@Override
			public void onPreExecute() {
				super.onPreExecute();
			}
			
			@Override
			public T doInBackground(final Void... params) {
				try {
					return pCallable.call();
				} catch (final Exception e) {
					this.mException = e;
				}
				return null;
			}
			
			@Override
			public void onPostExecute(final T result) {
				try {
				} catch (final Exception e) {
					Log.e("Error", e.toString());
				}
				if (this.isCancelled()) {
					this.mException = new CancelledException();
				}
				if (this.mException == null) {
					pCallback.onCallback(result);
				} else {
					if (pExceptionCallback == null) {
						if (this.mException != null)
							Log.e("Error", this.mException.toString());
					} else {
						pExceptionCallback.onCallback(this.mException);
					}
				}
				super.onPostExecute(result);
			}
		}.execute((Void[]) null);
		
	}

	/**
	 * 带进度条的异步处理
	 * @param <T>
	 * @param pContext
	 * @param pTitleResID
	 * @param pCallable
	 * @param pCallback
	 * @param pExceptionCallback
	 */
	public static <T> void doProgressDialogAsync(final Context pContext, final ProgressCallable<T> pCallable, final Callback<T> pCallback, final Callback<Exception> pExceptionCallback) {
		new AsyncTask<Void, Integer, T>() {
			private ProgressDialog mPD;
			private Exception mException = null;
			
			@Override
			public void onPreExecute() {
				this.mPD = new ProgressDialog(pContext);
				this.mPD.setIndeterminate(false);
				this.mPD.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				this.mPD.setMessage("处理中,请稍候...");
				this.mPD.show();
				super.onPreExecute();
			}
			
			@Override
			public T doInBackground(final Void... params) {
				try {
					return pCallable.call(new IProgressListener() {
						@Override
						public void onProgressChanged(final int pProgress) {
							onProgressUpdate(pProgress);
							Log.i("id", "ids "+pProgress);
							mPD.setMessage("处理中,请稍候...");
						}
					});
				} catch (final Exception e) {
					this.mException = e;
				}
				return null;
			}
			
			@Override
			public void onProgressUpdate(final Integer... values) {
				this.mPD.setProgress(values[0]);
				
			}

			@Override
			public void onPostExecute(final T result) {
				try {
					this.mPD.dismiss();
				} catch (final Exception e) {
					Log.e("Error", e.getLocalizedMessage());
				}
				if (this.isCancelled()) {
					this.mException = new CancelledException();
				}
				if (this.mException == null) {
					pCallback.onCallback(result);
				} else {
					if (pExceptionCallback == null) {
						Log.e("Error", this.mException.getLocalizedMessage());
					} else {
						pExceptionCallback.onCallback(this.mException);
					}
				}
				super.onPostExecute(result);
			}
		}.execute((Void[]) null);
		
	}
	
	
	
	
	public static class CancelledException extends Exception {
		private static final long serialVersionUID = -78123211381435595L;
	}
}
